﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ExpenseSystem.Core;
using ExpenseSystem.Core.Authority;
using ExpenseSystem.Core.RoleModules;
using ExpenseSystem.Fether;
using ExpenseSystem.Common;
using ExpenseSystem.Lor;
using ExpenseSystem.Model;
using ExpenseSystem.Common.BaseModel;
using ExpenseSystem.Core.Converter;

namespace ExpenseSystem.Controllers
{
    [RoutePrefix("api/Role")]
    public class RoleController : ApiController
    {
        SetInhibition setInhibition;
        GetInhibition getInhibition;
        SetInhibitionMaker inhibitionMaker = new StateInhibitionMaker();
        GetInhibitionMaker getInhibitionMaker = new GetInhibitionMaker();
        public RoleController()
        {
            setInhibition = inhibitionMaker.Make();
            getInhibition = getInhibitionMaker.Make();
        }
        [Route("List")]
        [HttpGet]
        public ResponseModel List([FromUri]RequestArgs paramModels)
        {
            return SystemControllerHelper.List(new RoleFetcher(), "SYS_ROLE_LIST", null, getInhibition);
        }
        [Route("ListModules")]
        [HttpGet]
        public ResponseModel ListModules(long roleId)
        {
            return SystemControllerHelper.ListDetail(new RoleModuleFetcher(), "SYS_ROLE_LIST", roleId, getInhibition);
        }
        [Route("ListSites")]
        [HttpGet]
        public ResponseModel ListSites(long roleId)
        {
            return SystemControllerHelper.ListDetail(new RoleSiteFetcher(), "SYS_ROLE_LIST", roleId, getInhibition);
        }
        [Route("ListUsers")]
        [HttpGet]
        public ResponseModel ListUsers(long roleId)
        {
            return SystemControllerHelper.ListDetail(new RoleUserFetcher(), "SYS_ROLE_LIST", roleId, getInhibition);

        }
        [Route("Get")]
        [HttpGet]
        public ResponseModel Get(long id)
        {
            return SystemControllerHelper.Get(new RoleFetcher(), "SYS_ROLE_LIST", id, getInhibition);
        }
        [Route("Set")]
        [HttpPost]
        public ResponseModel SetRole(RoleModel data)
        {
            return SystemControllerHelper.Set(new RoleFetcher(),"SYS_ROLE_SET",data,RoleManager.CreateInstance(), setInhibition);
        }
        
        [Route("SetUser")]
        [HttpPost]
        public ResponseModel SetUserRole([FromBody] RequestModel<RoleUserModel> requestData)
        {
            ResponseModel response = new ResponseModel();
            for (int i = 0; i < requestData.Data.Count(); i++)
            {
                if (requestData.Data[i].RoleId != requestData.MasterId)
                {
                    requestData.Data[i].RoleId = requestData.MasterId;
                }
            }
            RoleUserFetcher roleUserFetcher = new RoleUserFetcher();
            return SystemControllerHelper.SetDetail(roleUserFetcher, roleUserFetcher, "SYS_ROLE_SET", requestData, RoleManager.CreateInstance(), setInhibition);
        }
        [Route("SetSite")]
        [HttpPost]
        public ResponseModel SetSiteRole([FromBody] RequestModel<RoleSiteModel> requestData)
        {
            ResponseModel response = new ResponseModel();
            for (int i = 0; i < requestData.Data.Count(); i++)
            {
                if (requestData.Data[i].RoleId != requestData.MasterId)
                {
                    requestData.Data[i].RoleId = requestData.MasterId;
                }
            }
            RoleSiteFetcher roleSiteFetcher = new RoleSiteFetcher();
            return SystemControllerHelper.SetDetail(roleSiteFetcher, roleSiteFetcher, "SYS_ROLE_SET", requestData, RoleManager.CreateInstance(), setInhibition);
        }
        [Route("SetModule")]
        [HttpPost]
        public ResponseModel SetModuleRole([FromBody] RequestModel<RoleModuleModel> requestData)
        {
            ResponseModel response = new ResponseModel();
            for (int i = 0; i < requestData.Data.Count(); i++)
            {
                if (requestData.Data[i].RoleId != requestData.MasterId)
                {
                    requestData.Data[i].RoleId = requestData.MasterId;
                }
            }
            RoleModuleFetcher roleModuleFetcher = new RoleModuleFetcher();
            return SystemControllerHelper.SetDetail(roleModuleFetcher, roleModuleFetcher, "SYS_ROLE_SET", requestData, RoleManager.CreateInstance(), setInhibition);
        }
        [Route("Remove")]
        [HttpPost]
        public ResponseModel Remove(RoleModel data)
        {
            return SystemControllerHelper.Remove("SYS_ROLE_DEL", data, RoleManager.CreateInstance());
        }
    }
}
